package com.lagou.edu.course.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lagou.edu.course.api.dto.CourseDTO;
import com.lagou.edu.course.api.param.CourseQueryParam;
import com.lagou.edu.course.remote.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程Controller
 *
 * @author lm
 * @since 2021/10/25 11:09
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    CourseService courseService;

    @GetMapping("/getAllCourse")
    public List<CourseDTO> getAllCourse(@RequestParam(required = false, name = "userId") Integer userId) {
        return courseService.getAllCourse(userId);
    }

    @GetMapping("/getCourseById")
    public CourseDTO getCourseById(@RequestParam("courseId") Integer courseId, @RequestParam(required = false, name = "userId") Integer userId) {
        return courseService.getCourseById(courseId, userId);
    }

    /**
     * 获取已购课程信息
     *
     * @param userId 用户ID
     * @return 已购课程
     */
    @GetMapping("/getPurchasedCourse")
    List<CourseDTO> getPurchasedCourse(@RequestParam("userId") Integer userId) {
        return courseService.getPurchasedCourse(userId);
    }

    /**
     * 添加或更新课程
     *
     * @param courseDTO 课程信息
     * @return 更新结果
     */
    @PostMapping(value = "/saveOrUpdateCourse", consumes = "application/json")
    boolean saveOrUpdateCourse(@RequestBody CourseDTO courseDTO) {
        return courseService.saveOrUpdateCourse(courseDTO);
    }

    @GetMapping("/changeState")
    boolean changeState(@RequestParam("courseId") Integer courseId, @RequestParam("status") Integer status) {
        return courseService.changeState(courseId, status);
    }

    /**
     * 分页查询课程
     *
     * @param courseQueryParam 查询参数
     * @return 课程
     */
    @PostMapping("/getQueryCourses")
    Page<CourseDTO> getQueryCourses(@RequestBody CourseQueryParam courseQueryParam) {
        return courseService.getQueryCourses(courseQueryParam);
    }
}
